﻿using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

namespace web_uchet.Classes.Security
{
    public class EnterCharsHandler : IHttpHandler
    {
        /// <summary>
        /// You will need to configure this handler in the web.config file of your 
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return true; }
        }
        private static LinearGradientBrush _Brush;
        private static Pen _Pen;
        private static Random R = new Random();

        static EnterCharsHandler()
        {
            //Point[] ptGraph = new Point[]{ new  Point(10, 10),
            //new Point(50, 50),
            //new Point(10, 30),
            //new Point(40, 79) };
            //EnterCharsHandler._Brush = new PathGradientBrush(ptGraph);
            //_Brush.CenterColor = Color.LightPink;
            //_Brush.SurroundColors[0] = Color.Black;
            _Brush = new LinearGradientBrush(new Point(30, 0), new Point(40, 90), Color.Gainsboro, Color.Gray);
            _Pen = new Pen(Brushes.Black, 3);

        }

        public void ProcessRequest(HttpContext context)
        {
            //write your handler implementation here.
            //context.Response.Write("EnterCharsHandler");
            Bitmap Img = new Bitmap(200, 60);
            
            Graphics Gr= Graphics.FromImage(Img);
            //Gr.FillRectangle(_Brush, new Rectangle(0, 0, 200, 100));

            string EncStr = _3DesAlghImplem.DecryptData(
                context.Request.QueryString["src"]
                //.Replace(" ","+")
                );            
            Matrix M = new Matrix(0.8f, 0.1f, 0.5f, 0.8f, 0.6f, 0.3f);
            Gr.RotateTransform(45.0f,
            System.Drawing.Drawing2D.MatrixOrder.Prepend);
            //Gr.TranslateTransform(50, 0);
            Gr.Transform = M;

            Gr.DrawString(EncStr, new System.Drawing.Font("Arial", 40), _Brush, new RectangleF(0, 0, 200, 60));
            for (int i=0;i<R.Next(15);i++)
                Gr.DrawLine(_Pen, new Point(R.Next(160), R.Next(60)), new Point(R.Next(160), R.Next(60)));

            for (int j = 0; j < 800; j++)
                Img.SetPixel(R.Next(0, 200), R.Next(0, 60), Color.Gainsboro);

            Img.Save(context.Response.OutputStream, ImageFormat.Gif);            
            context.Response.ContentType = "image/gif";            
        }

        #endregion
    }
}
